package com.example.demo.dao;

import com.example.demo.model.Staff;
import com.example.demo.util.StringUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StaffDao extends BaseDao {

    //展示员工列表，按员工姓名模糊查询
    public List<Staff> getStaffList(Staff staff ){
        List<Staff> retList= new ArrayList<Staff>();
        StringBuffer sqlString = new StringBuffer("select * from emp");
        if(!StringUtil.isEmpty(staff.getYname())){
            sqlString.append(" and yname like '%"+staff.getYname()+"%'");
        }
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString().replaceFirst("and", "where"));
            ResultSet executeQuery = preparedStatement.executeQuery();
            while(executeQuery.next()){
                Staff s = new Staff();
                s.setYno(executeQuery.getInt("yno"));
                s.setYname(executeQuery.getString("yname"));
                s.setSex(executeQuery.getString("sex"));
                s.setBirth(executeQuery.getString("birth"));
                s.setHome(executeQuery.getString("home"));
                s.setDegree(executeQuery.getString("degree"));
                s.setSalary(executeQuery.getDouble("salary"));
                s.setInfo(executeQuery.getString("info"));
                retList.add(s);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return retList;
    }
    //删除员工信息
    public boolean delete(int yno){
        String sql = "delete from emp where yno=?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1, yno);
            if(preparedStatement.executeUpdate() > 0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    //修改员工信息
    public boolean update(Staff staff){
        String sql = "update emp set yname=?,sex=?,birth=?,home=?,degree=?,salary=?,info=? where yno=?";
        try {
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, staff.getYname());
            preparedStatement.setString(2, staff.getSex());
            preparedStatement.setString(3, staff.getBirth());
            preparedStatement.setString(4, staff.getHome());
            preparedStatement.setString(5, staff.getDegree());
            preparedStatement.setDouble(6, staff.getSalary());
            preparedStatement.setString(7, staff.getInfo());
            preparedStatement.setInt(8, staff.getYno());
            if(preparedStatement.executeUpdate() > 0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    //增加员工信息
    public boolean addStaff(Staff staff){
        String sql = "insert into emp values(null,?,?,?,?,?,?,?)";
        try {
            java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setString(1, staff.getYname());
            preparedStatement.setString(2, staff.getSex());
            preparedStatement.setString(3, staff.getBirth());
            preparedStatement.setString(4, staff.getHome());
            preparedStatement.setString(5, staff.getDegree());
            preparedStatement.setDouble(6, staff.getSalary());
            preparedStatement.setString(7, staff.getInfo());
            if(preparedStatement.executeUpdate() > 0)return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
